-- drop function if exists sm_sc.fv_sgn_ifft2(sm_sc.typ_l_complex[]);
create or replace function sm_sc.fv_sgn_ifft2
(
  i_array          sm_sc.typ_l_complex[]
)
returns sm_sc.typ_l_complex[]
as
$$
-- -- -- declare
-- -- -- 如果要返回任意长度，要做循环卷积改造 https://blog.csdn.net/weixin_34128237/article/details/94637989
-- -- 
-- -- begin
-- --   if array_ndims(i_array) = 2
-- --   then
-- --     return
-- --     (
-- --       with
-- --       cte_fft_x as
-- --       (
-- --       select 
-- --         array_agg(sm_sc.fv_sgn_ifft(array(select unnest(i_array[a_no_y:a_no_y][:]))) order by a_no_y) as a_fft_x
-- --       from generate_series(1, array_length(i_array, 1)) tb_a_y(a_no_y)    
-- --       )
-- --       select 
-- --         |^~| array_agg(sm_sc.fv_sgn_ifft(array(select unnest(a_fft_x[:][a_no_x:a_no_x]))) order by a_no_x) as a_fft_x
-- --       from generate_series(1, power(2, ceil(log(2, array_length(i_array, 2)))) :: int) tb_a_x(a_no_x), cte_fft_x
-- --     )
-- --     ;
-- --   else
-- --     raise exception 'error dim!';
-- --   end if;
-- -- end 
declare 
  v_ifft2   float[]    :=    
    sm_sc.__fv_sgn_ifft2_py
    (
      sm_sc.fv_opr_real(i_array)
    , sm_sc.fv_opr_imaginary(i_array)
    )
  ;
begin 
  return 
    sm_sc.fv_mx_descend_dim_py(v_ifft2[1 : 1]) :: sm_sc.typ_l_complex[] 
    +` 
    (sm_sc.fv_mx_descend_dim_py(v_ifft2[2 : 2]) :: sm_sc.typ_l_complex[] *` ((0,1) :: sm_sc.typ_l_complex))
  ;
end
$$
language plpgsql stable
cost 100;

-- select sm_sc.fv_sgn_ifft2
-- (
-- array
-- [ 
--   array[1,2,3,4,5,6,7,8,9,10,11,12],
--   array[2,3,4,5,6,7,8,9,10,11,12,13],
--   array[3,4,5,6,7,8,9,10,11,12,13,14],
--   array[4,5,6,7,8,9,10,11,12,13,14,15],
--   array[5,6,7,8,9,10,11,12,13,14,15,16]
-- ]
-- ):: sm_sc.typ_l_complex[]
-- -- 预期结果：
-- -- {{"(3.98437500,0.00000000)","(-1.67084304,-0.33095601)","(0.21819479,-0.84874696)","(0.46541353,0.17363807)","(-0.23437501,0.23437500)","(-0.15128733,-0.31799424)","(0.32868023,-0.14562195)","(0.10671684,0.27116173)","(-0.23437500,0.00000000)","(0.10671685,-0.27116173)","(0.32868022,0.14562196)","(-0.15128733,0.31799424)","(-0.23437500,-0.23437500)","(0.46541352,-0.17363806)","(0.21819479,0.84874695)","(-1.67084303,0.33095600)"}
-- -- ,{"(-0.50758252,-1.92382643)","(-0.07462441,0.93422823)","(-0.45210983,-0.00323604)","(0.03103848,-0.21421868)","(0.11316626,0.11316628)","(-0.16055899,0.10832875)","(-0.11261105,-0.14118023)","(0.10557234,-0.06846987)","(0.00000000,0.11316626)","(-0.15628459,-0.03458503)","(0.02801396,-0.17622148)","(0.14652342,0.03776722)","(-0.11316626,0.11316626)","(-0.13664126,-0.23522438)","(0.36751274,-0.20747148)","(0.24497499,0.67928054)"}
-- -- ,{"(0.79687500,0.18750000)","(-0.28708002,-0.09765479)","(0.08136105,-0.15412439)","(0.09696244,0.05423235)","(-0.04687500,0.04687500)","(-0.01722482,-0.06100650)","(0.07220813,-0.01349939)","(0.01508487,0.06359885)","(-0.04687500,0.00000000)","(0.02760187,-0.04486584)","(0.05926396,0.04474939)","(-0.04329012,0.06619121)","(-0.04687500,-0.04687500)","(0.08920297,-0.01522288)","(0.00591687,0.18537439)","(-0.38125720,0.03472761)"}
-- -- ,{"(-0.24241748,-0.33007643)","(0.01326177,0.19929769)","(-0.09051396,0.03069479)","(-0.01083289,-0.03354004)","(0.01941626,0.01941627)","(-0.02969515,0.02938287)","(-0.03226518,-0.01886105)","(0.01035389,-0.01693228)","(0.00000000,0.01941626)","(-0.03457366,-0.00074915)","(-0.00813775,-0.03559648)","(0.02299187,-0.00431677)","(-0.01941626,0.01941626)","(-0.03960219,-0.04357220)","(0.05011105,-0.06684648)","(0.06809636,0.07753665)"}
-- -- ,{"(0.79687500,0.00000000)","(-0.33416861,-0.06619120)","(0.04363896,-0.16974939)","(0.09308271,0.03472762)","(-0.04687500,0.04687500)","(-0.03025747,-0.06359885)","(0.06573605,-0.02912439)","(0.02134337,0.05423234)","(-0.04687500,0.00000000)","(0.02134337,-0.05423235)","(0.06573605,0.02912439)","(-0.03025747,0.06359885)","(-0.04687500,-0.04687500)","(0.09308270,-0.03472761)","(0.04363896,0.16974939)","(-0.33416861,0.06619120)"}
-- -- ,{"(-0.24241748,0.33007643)","(0.06809636,-0.07753665)","(0.05011104,0.06684648)","(-0.03960219,0.04357220)","(-0.01941626,-0.01941627)","(0.02299187,0.00431677)","(-0.00813775,0.03559648)","(-0.03457366,0.00074914)","(0.00000000,-0.01941626)","(0.01035388,0.01693228)","(-0.03226518,0.01886104)","(-0.02969515,-0.02938287)","(0.01941626,-0.01941626)","(-0.01083289,0.03354004)","(-0.09051396,-0.03069478)","(0.01326177,-0.19929769)"}
-- -- ,{"(0.79687500,-0.18750000)","(-0.38125720,-0.03472761)","(0.00591687,-0.18537439)","(0.08920298,0.01522288)","(-0.04687500,0.04687500)","(-0.04329011,-0.06619121)","(0.05926396,-0.04474939)","(0.02760187,0.04486584)","(-0.04687500,0.00000000)","(0.01508487,-0.06359885)","(0.07220814,0.01349939)","(-0.01722482,0.06100650)","(-0.04687500,-0.04687500)","(0.09696244,-0.05423235)","(0.08136104,0.15412439)","(-0.28708002,0.09765479)"}
-- -- ,{"(-0.50758252,1.92382643)","(0.24497500,-0.67928055)","(0.36751275,0.20747149)","(-0.13664127,0.23522438)","(-0.11316626,-0.11316628)","(0.14652343,-0.03776722)","(0.02801395,0.17622149)","(-0.15628459,0.03458502)","(0.00000000,-0.11316626)","(0.10557234,0.06846988)","(-0.11261105,0.14118022)","(-0.16055898,-0.10832875)","(0.11316626,-0.11316626)","(0.03103848,0.21421868)","(-0.45210983,0.00323605)","(-0.07462440,-0.93422822)"}}
